import 'package:flutter/material.dart';
import 'package:secend_flutter/bean/home_common_model_bean.dart';
import 'package:secend_flutter/bean/home_grid_nav_model_bean.dart';
import 'package:secend_flutter/dao/tao_ean.dart';
import 'package:secend_flutter/define_widget/webview_widget.dart';
import 'package:secend_flutter/main.dart';

class GridNavitor extends StatelessWidget {
  final GridNavBean? gridNavBean;

   const GridNavitor({Key? key,  this.gridNavBean}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return PhysicalModel(
      color: Colors.transparent,
      borderRadius: BorderRadius.circular(8),
      clipBehavior: Clip.antiAlias,//裁剪属性 裁剪且抗锯齿
      child:  Column(
        children: _gridItems(context),
      ),
    );
  }

  List<Widget> _gridItems(BuildContext context) {
    List<Widget> items = [];
    if (gridNavBean == null) {
      return items;
    }
    items.add(_gridItem(context, gridNavBean!.hotel, true));
    items.add(_gridItem(context, gridNavBean!.flight, false));
    items.add(_gridItem(context, gridNavBean!.travel, false));
    return items;
  }

  Widget _gridItem(BuildContext context, GridNavItem gridNavItem, bool one) {
    List<Widget> items = [];
    items.add(_mainItem(context, gridNavItem.mainItem));
    items.add(_smallItem(context, gridNavItem.item1, gridNavItem.item2));
    items.add(_smallItem(context, gridNavItem.item3, gridNavItem.item4));
    Color startBycolor=Color(int.parse('0xff'+gridNavItem.startColor));
    Color endBycolor=Color(int.parse('0xff'+gridNavItem.endColor));
    return Container(
      height: 88,
      margin: one? null:const EdgeInsets.only(top: 3),
      decoration: BoxDecoration(
        //渐变属性
        gradient:LinearGradient(colors: [startBycolor,endBycolor]),
      ),
      child: Row(children: items,),
    );
  }

  Widget _mainItem(BuildContext context, CommonBean mainItem) {
    return _gestureWrap(
        context,
        mainItem, Stack(
              alignment: Alignment.topCenter,
              children: [
                Image.network(
                  mainItem.icon!,
                  fit: BoxFit.contain,
                  height: 88,
                  width: 121,
                  alignment: AlignmentDirectional.bottomEnd,
                ),
                Container(
                  margin: const EdgeInsets.only(top: 11),
                  child: Text(
                    mainItem.title!,
                    style: const TextStyle(fontSize: 16, color: Colors.white),
                  ),
                )
              ],
            ),);
  }

  Widget _smallItem(BuildContext context, CommonBean item1, CommonBean item2) {
    return Expanded(
        child: Column(
          children: [
            Expanded(child: _item(context, item1, true),flex: 1,),
            Expanded(child: _item(context, item2, false),flex: 1,),
          ],
        ),
        flex: 1);
  }

  Widget _item(BuildContext context, CommonBean mainItem, bool one) {
    BorderSide side = const BorderSide(width: 0.8, color: Colors.white);
    return FractionallySizedBox(
      widthFactor: 1,
      child: Container(
        decoration: BoxDecoration(
            border: Border(left: side, bottom: one ? side : BorderSide.none)),
        child: _gestureWrap(
            context,
            mainItem,
            Center(
              child: Text(
                mainItem.title!,
                textAlign: TextAlign.center,
                style: const TextStyle(fontSize: 16, color: Colors.white),
              ),
            )),
      ),
    );
  }

  Widget _gestureWrap(
      BuildContext context, CommonBean mainItem, Widget widget) {
    return GestureDetector(
      onTap: () {
        Tiaboon.push(
            navigatorKey.currentState!.overlay!.context,//覆盖
            MyWebView(
              url: mainItem.url,
              statusBarColor: mainItem.statusBarColor,
              hideAppBar: mainItem.hideAppBar,
              title: mainItem.title,
            ));
      },
      child: widget,
    );
  }
}
